#include "re.h"

char * re_copy(RE *re, int match, char *string) {

   int size;
   char *strout;

   size = re->vec[2*match+1] - re->vec[2*match];
   strout = (char *) malloc(size+1);
   sprintf(strout, "%.*s", size, string + re->vec[2*match]);
   return strout;

}

/* *****************************
 * funcao: matcher 
 * Faz uma busca usando expressao regular compativeis de perl 
 * retornando vetor com ponteiros onde a expressao foi encontrada.
 * Returna 0 com sucesso e -1 sem sucesso
 * ***************************** */
int matcher(char *pattern, char *search, RE *re) {

   pcre *regex;
   const char *errptr;
   int err,exec;

   regex = pcre_compile(pattern, RE_COMP_TYPE, &errptr, &err, NULL);
   if(regex == NULL)
      return -1;

   exec = pcre_exec(regex, NULL, search, strlen(search), 0, RE_EXEC_TYPE, re->vec, (int) sizeof(re->vec)/4 );
   if(exec < 0) {
      free(regex);
      return -1;
   }

   free(regex);
   return 0;

}
